package com.xiaobaibai.controller;

import com.xiaobaibai.common.OrderCommon;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IOrderService;
import com.xiaobaibai.service.IProductService;
import com.xiaobaibai.service.IUserService;
import com.xiaobaibai.units.Last7DaysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "仪盘表接口")
@RequestMapping("/dashboard")
@CrossOrigin
public class DashboardController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/index")
    @ApiOperation("仪盘表数据接口")
    public ResponseFormat getDashboardData(){
        /**
         * 1.会员数量
         * 2.商品数量
         * 3.已完成订单数量  sum函数
         * 4.销售额         sum函数
         * 5.近七日完成订单数量和近七日的销售额
         */
        int userCount = userService.count();
        int productCount = productService.count();
        Map okSum = orderService.getDashboard();

        List<String> recent7Day = Last7DaysUtil.getRecent7Day();

        List<Object> list = stringRedisTemplate.executePipelined(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                //以前->限制
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(6));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(5));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(4));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(3));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(2));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(1));
                redisOperations.opsForValue().get(OrderCommon.orderCount + recent7Day.get(0));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(6));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(5));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(4));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(3));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(2));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(1));
                redisOperations.opsForValue().get(OrderCommon.orderAmount + recent7Day.get(0));
                return null;
            }
        });

        List<Integer> orderAmount=new ArrayList<>(7);
        List<Integer> recentOrder=new ArrayList<>(7);
        for (int i = 0; i < list.size(); i++) {
            if(i>6)
                recentOrder.add(list.get(i)==null?null:  Integer.parseInt((String) list.get(i)));
            else
                orderAmount.add(list.get(i)==null?null:  Integer.parseInt((String) list.get(i)));
        }

        Map<String,Object> map=new HashMap<>();
        map.put("userCount",userCount);
        map.put("productCount",productCount);
        map.put("okSum",okSum);
        map.put("orderAmount",orderAmount);
        map.put("recentOrder",recentOrder);
        map.put("day",recent7Day);

        return ResponseFormat.ok("获取仪盘表数据成功",map);
    }

}
